import requests
from bs4 import BeautifulSoup

agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36 Edg/85.0.564.44'
headers = {
    'User-Agent': agent
}


class Reptiles(object):
    def __init__(self, url, header=None):
        self.url = url
        self.header = header

    def send_request(self, code='utf-8'):
        response = requests.get(self.url, self.header)
        return response.content.decode(code)

    def catch_data(self, select):
        html_data = self.send_request()
        soup = BeautifulSoup(html_data, 'lxml')
        data_list = soup.select(select)
        print(len(data_list))
        data = []
        data_dict = {}
        for i in data_list:
            data_dict['title'] = i.get_text()
            data_dict['url'] = i.get('href')
            data.append(data_dict)
        print(data)
        return data

    def save_data(self, data, filename, code='utf-8'):
        with open(filename, 'w', encoding=code) as file:
            file.write(data)


if __name__ == '__main__':
    num = int(input('请输入要获取的页数：'))
    for i in range(1, num+1):
        url = 'http://mil.news.sina.com.cn/roll/index.d.html?cid=57918&page={}'.format(i)
        reptiles = Reptiles(url, headers)
        reptiles.save_data(str(reptiles.catch_data('.linkNews li a')), 'military' + str(i))
